﻿using SuperSocket.SocketBase.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestLog4Net;

namespace ETS_FinancialData_sync_toolsForServer
{
    public class TcpServer
    {
        private TelnetServer _TcpServer;

        public TcpServer()
        {
            _TcpServer = new TelnetServer();
            _TcpServer.NewSessionConnected += _TcpServer_NewSessionConnected;
        }

        void _TcpServer_NewSessionConnected(TelnetSession session)
        {
            session.Send(" welcome##欢迎来到socketServer||");
        }

        public void Start()
        {
            var serverConfig = new ServerConfig
            {
                Name = "TestServer",// "AgileServer",//服务器实例的名称
                //ServerType = "AgileServer.Socket.TelnetServer, AgileServer.Socket",
                Ip = "Any",//Any - 所有的IPv4地址 IPv6Any - 所有的IPv6地址
                Mode = SuperSocket.SocketBase.SocketMode.Tcp,//服务器运行的模式, Tcp (默认) 或者 Udp
                Port = 10100,//服务器监听的端口
                SendingQueueSize = 10,//发送队列最大长度, 默认值为5
                MaxConnectionNumber = 10000,//可允许连接的最大连接数
                LogCommand = false,//是否记录命令执行的记录
                LogBasicSessionActivity = false,//是否记录session的基本活动，如连接和断开
                LogAllSocketException = false,//是否记录所有Socket异常和错误
                //Security = "tls",//Empty, Tls, Ssl3. Socket服务器所采用的传输层加密协议
                MaxRequestLength = 10240,//最大允许的请求长度，默认值为1024
                TextEncoding = "UTF-8",//文本的默认编码，默认值是 ASCII
                KeepAliveTime = 6000,//网络连接正常情况下的keep alive数据的发送间隔, 默认值为 600, 单位为秒
                KeepAliveInterval = 600,//Keep alive失败之后, keep alive探测包的发送间隔，默认值为 60, 单位为秒
                ClearIdleSession = true, // 是否定时清空空闲会话，默认值是 false;
                ClearIdleSessionInterval = 30//: 清空空闲会话的时间间隔, 默认值是120, 单位为秒;
            };

            var rootConfig = new RootConfig()
            {
                MaxWorkingThreads = 1000,//线程池最大工作线程数量
                MinWorkingThreads = 10,// 线程池最小工作线程数量;
                MaxCompletionPortThreads = 1000,//线程池最大完成端口线程数量;
                MinCompletionPortThreads = 10,// 线程池最小完成端口线程数量;
                DisablePerformanceDataCollector = true,// 是否禁用性能数据采集;
                PerformanceDataCollectInterval = 60,// 性能数据采集频率 (单位为秒, 默认值: 60);
                LogFactory = "ConsoleLogFactory",//默认logFactory的名字
                Isolation = SuperSocket.SocketBase.IsolationMode.AppDomain// 服务器实例隔离级别                
            };

            try
            {
                _TcpServer.Setup(rootConfig, serverConfig);
                _TcpServer.Start();
                LogHelper.WriteLog(typeof(TcpServer),string.Format("Tcp - 启动=>地址[{0}], 端口[{1}]", "127.0.0.1", _TcpServer.Config.Port));
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Tcp 启动失败，{0}", e.Message));
            }
        }

        public void Stop()
        {
            _TcpServer.Stop();
        }


    }
}
